﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using common;
using logistical.Models;

namespace logistical.Controllers
{
    public class weixinLoginController : ApiController
    {
        [HttpGet]
        public object autoLogin(string openid)
        {
            var o = new logisticalEntities();
            var p = o.sys_userinfo.Where(x => x.openid == openid);
            if (p.Count() == 1)
            {
                var q = p.FirstOrDefault();
                loginController lc = new loginController();
                return lc.login(q.username, q.password);
            }
            return new { msg = "error", code = "", url = "" };
        }

        [HttpGet]
        public object login(string username, string pwd, string openid)
        {
            var o = new logisticalEntities();
            var p = o.sys_userinfo.Where(x => x.openid == openid);
            if (p.Count() == 0)
            {
                var q = o.sys_userinfo.Where(x => x.username == username && x.password == pwd);
                if (q.Count() != 1) {
                    return new { msg = "error", code = "", url = "" };
                }
                q.FirstOrDefault().openid = openid;
                o.SaveChanges();
            }
            if (p.Count() == 1)
            {
                if (p.FirstOrDefault().username != username) {
                    return new { msg = "error", code = "", url = "" };
                }
            }
            loginController lc = new loginController();
            return lc.login(username, pwd);
        }
    }
}
